package com.project.xsf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.common.entity.PageVo;
import com.project.xsf.entity.MDesignProcedure;
import com.project.xsf.entity.MDesignProcedureDetails;
import com.project.xsf.entity.MDesignProcedureModule;
import com.project.xsf.mapper.MDesignProcedureDetailsMapper;
import com.project.xsf.mapper.MDesignProcedureMapper;
import com.project.xsf.mapper.MDesignProcedureModuleMapper;
import com.project.xsf.service.MDesignProcedureModuleService;
import com.project.xy.entity.DModuleDetails;
import com.project.xy.mapper.DModuleDetailsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class MDesignProcedureModuleImpl implements MDesignProcedureModuleService {
    @Autowired
    private MDesignProcedureModuleMapper mDesignProcedureModuleMapper;
    @Autowired
    private DModuleDetailsMapper dModuleDetailsMapper;
    @Autowired
    private MDesignProcedureMapper mDesignProcedureMapper;
    @Autowired
    private MDesignProcedureDetailsMapper mDesignProcedureDetailsMapper;
    @Override
    public int deleteByPId(Integer pId) {
        return mDesignProcedureModuleMapper.deleteByPId(pId);
    }

    @Override
    public int insertSelective(MDesignProcedureModule record) {
        return mDesignProcedureModuleMapper.insertSelective(record);
    }

    @Override
    public int addMDesignProcedureModule(MDesignProcedureModule[] mDesignProcedureModules, DModuleDetails[] dModuleDetails, MDesignProcedureDetails mDesignProcedureDetail,Integer mDesignProcedureDetailId) {
//        重新设计时将删除该工序对应的物料信息
        mDesignProcedureModuleMapper.deleteByPId(mDesignProcedureDetailId);
        for(int i=0;i<mDesignProcedureModules.length;i++){
            //往产品生产工序物料明细表插数据
            MDesignProcedureModule mDesignProcedureModule = mDesignProcedureModules[i];
            mDesignProcedureModuleMapper.insertSelective(mDesignProcedureModule);
            //修改产品生产工序明细表的工序物料标志；D002-1: 已设计
            //产品生产工序物料明细表的parentId为M_DESIGN_PROCEDURE的ID相对应
            MDesignProcedureDetails mDesignProcedureDetails = new MDesignProcedureDetails();
            mDesignProcedureDetails.setId(mDesignProcedureModule.getParentId());
            mDesignProcedureDetails.setDesignModuleTag("D002-1");
//            System.out.println(mDesignProcedureDetails);
            mDesignProcedureDetailsMapper.updateByPrimaryKeySelective(mDesignProcedureDetails);

            //修改产品物料组成明细表的可用数量：
            DModuleDetails dModuleDetail = dModuleDetails[i];
//            System.out.println(dModuleDetail);
//            System.out.println(mDesignProcedureModule);
            dModuleDetailsMapper.updateByPrimaryKeySelective(dModuleDetail);
        }
        //修改产品生产工序明细表的物料总成本
        mDesignProcedureDetailsMapper.updateByPrimaryKeySelective(mDesignProcedureDetail);
        return 0;
    }

    @Override
    public List<MDesignProcedureModule> queryMDesignProcedureModuleByPId(Integer pId) {
        return mDesignProcedureModuleMapper.queryMDesignProcedureModuleByPId(pId);
    }

    @Override
    public PageVo<MDesignProcedureModule> queryMDesignProcedureModuleByPId(Map<String, String> map) {
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize= Integer.valueOf(map.get("pageSize"));
//        System.out.println(map);
        PageHelper.startPage(pageNum,pageSize);
        List<MDesignProcedureModule> MDesignProcedureModules = mDesignProcedureModuleMapper.queryMDesignProcedureModuleByPId(Integer.valueOf(map.get("pId")));
        PageInfo<MDesignProcedureModule> pageInfo = new PageInfo<MDesignProcedureModule>(MDesignProcedureModules);

        PageVo<MDesignProcedureModule> pageVo = new PageVo<MDesignProcedureModule>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }

    @Override
    public int deleteByMDesignProcedureId(Integer mDesignProcedureId) {
        return mDesignProcedureDetailsMapper.deleteByMDesignProcedureId(mDesignProcedureId);
    }

}
